﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.PmiScale.CUiPMIScaleFontSizeCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using DnProcessSimulateCommands.PMI.PmiScale.Resources;
using System;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.PmiScale;

public class CUiPMIScaleFontSizeCmd : TxComboBoxButtonCommand
{
  private string[] m_sizes;
  private string m_fontLabel;
  private string m_unitLabel = "pt";
  private TxPmi m_selectedPmi;
  private bool m_scaling;
  private CApPMIScaleEnabler m_pmiScaleEnabler;
  private CApPMIScaleApp m_pmiScale;

  public CUiPMIScaleFontSizeCmd()
  {
    this.m_fontLabel = PMIScaleStringTable.FONT_LABEL;
    this.m_pmiScale = new CApPMIScaleApp();
    this.m_pmiScaleEnabler = new CApPMIScaleEnabler((TxPmiTextDisplayMode) 1);
    this.m_sizes = new string[this.m_pmiScale.FontSizes.Length];
    int num = 0;
    foreach (int fontSiz in this.m_pmiScale.FontSizes)
      this.m_sizes[num++] = fontSiz.ToString();
    this._items = this.m_sizes;
    this._text = this.m_fontLabel;
    this._selectedIndex = -1;
  }

  public virtual bool Connect()
  {
    this.RegisterSelectionEvents();
    return true;
  }

  public virtual void Disconnect()
  {
    this.UnRegisterSelectionEvents();
    this.UnregisterPmiScalingEvents();
  }

  public virtual void Execute(object cmdParams)
  {
    int fontSize = 0;
    try
    {
      fontSize = this.ParseToInt(cmdParams as string);
    }
    catch
    {
    }
    if (fontSize >= this.m_pmiScale.FontSizeMinVal && fontSize <= this.m_pmiScale.FontSizeMaxVal)
    {
      this.m_scaling = true;
      this.m_pmiScale.SetFontSizeForSelection(fontSize);
      this.m_scaling = false;
      this._text = $"{this.m_fontLabel} {fontSize.ToString()} {this.m_unitLabel}";
      this._selectedIndex = this.FindIndex(fontSize.ToString());
      this.FireUpdateItemsRequested();
    }
    else
      this.UpdateEditBoxText();
  }

  public virtual string Category => PMIScaleStringTable.PMI_SCALE_CATEGORY;

  public virtual string Name => PMIScaleStringTable.PMI_FONT_SIZE;

  public virtual string Description => PMIScaleStringTable.PMI_FONT_SIZE_DESCRIPTION;

  public virtual string Tooltip => PMIScaleStringTable.PMI_FONT_SIZE_TOOLTIP;

  public virtual bool Licensed => TxCommandLicenser.SelectLayoutLicense;

  public virtual bool ShowMissingLicenseMessage => true;

  public virtual bool ReadOnly => false;

  public virtual int Width
  {
    get
    {
      int width = base.Width;
      try
      {
        width = int.Parse(PMIScaleStringTable.PMI_FONT_SIZE_WIDTH);
      }
      catch
      {
      }
      return width;
    }
  }

  public virtual ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_pmiScaleEnabler;

  private void RegisterSelectionEvents()
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    activeSelection.ItemsSet += new TxSelection_ItemsSetEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsAdded += new TxSelection_ItemsAddedEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsRemoved += new TxSelection_ItemsRemovedEventHandler(this.OnSelectionChanged);
    activeSelection.Cleared += new TxSelection_ClearedEventHandler(this.OnSelectionChanged);
  }

  private void UnRegisterSelectionEvents()
  {
    TxSelection activeSelection = TxApplication.ActiveSelection;
    activeSelection.ItemsSet -= new TxSelection_ItemsSetEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsAdded -= new TxSelection_ItemsAddedEventHandler(this.OnSelectionChanged);
    activeSelection.ItemsRemoved -= new TxSelection_ItemsRemovedEventHandler(this.OnSelectionChanged);
    activeSelection.Cleared -= new TxSelection_ClearedEventHandler(this.OnSelectionChanged);
  }

  private void OnSelectionChanged(object sender, EventArgs args)
  {
    this.RegisterPmiScalingEvents();
    this.UpdateEditBoxText();
  }

  private void RegisterPmiScalingEvents()
  {
    this.UnregisterPmiScalingEvents();
    if (!TxApplication.ActiveSelection.HasSingleSelectionInAllItems())
      return;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count <= 0 || !(((Collection<ITxObject>) allItems)[0] is TxPmi txPmi) || txPmi.TextDisplayMode != 1)
      return;
    this.m_selectedPmi = txPmi;
    this.m_selectedPmi.TextScaled += new TxPmi_TextScaledEventHandler(this.OnTextScaled);
  }

  private void UnregisterPmiScalingEvents()
  {
    if (this.m_selectedPmi == null)
      return;
    this.m_selectedPmi.TextScaled -= new TxPmi_TextScaledEventHandler(this.OnTextScaled);
  }

  private void OnTextScaled(object sender, EventArgs args)
  {
    if (this.m_scaling)
      return;
    this.UpdateEditBoxText();
  }

  private void UpdateEditBoxText()
  {
    this._text = this.m_fontLabel;
    this._selectedIndex = -1;
    int sizeFromSelection = this.m_pmiScale.GetFontSizeFromSelection();
    if (sizeFromSelection >= 0)
    {
      this._text = $"{this._text} {sizeFromSelection.ToString()} {this.m_unitLabel}";
      this._selectedIndex = this.FindIndex(sizeFromSelection.ToString());
    }
    this.FireUpdateItemsRequested();
  }

  private int ParseToInt(string strValue)
  {
    string lower1 = strValue.ToLower();
    string lower2 = this.m_fontLabel.ToLower();
    string lower3 = this.m_unitLabel.ToLower();
    return int.Parse(lower1.Replace(lower2, "").Replace(lower3, "").Replace(" ", ""));
  }

  private int FindIndex(string item)
  {
    int index = 0;
    foreach (string siz in this.m_sizes)
    {
      if (siz == item)
        return index;
      ++index;
    }
    return -1;
  }
}
